import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial.distance import cdist

# 教学楼尺寸
building_width = 100
building_length = 80
building_floors = 3
floor_height = 3.5

# 墙体衰减
wall_attenuation = {
    "承重墙": 12,
    "普通隔断墙": 6,
    "玻璃幕墙": 7
}

# AP 覆盖半径
ap_coverage = {
    "2.4GHz": {"无墙体": 15, "穿透1堵承重墙": 8},
    "5GHz": {"无墙体": 10, "穿透1堵承重墙": 5}
}

# 目标信号强度阈值
signal_threshold = -65

# 教学楼布局
classrooms = [(x, y) for x in range(10, 90, 15) for y in range(10, 70, 10)]
corridor = [(50, y) for y in range(0, 80, 2)]

# AP 部署函数
def deploy_ap():
    ap_positions = []
    for floor in range(building_floors):
        # 每层中心部署一个 AP
        ap_positions.append((50, 40, floor * floor_height))
    return ap_positions

# 信号强度计算函数
def calculate_signal_strength(ap_positions):
    x = np.linspace(0, building_width, 100)
    y = np.linspace(0, building_length, 80)
    X, Y = np.meshgrid(x, y)
    signal_strength = np.zeros_like(X)

    for ap in ap_positions:
        distance = np.sqrt((X - ap[0])**2 + (Y - ap[1])**2)
        signal = -30 - 20 * np.log10(distance)  # 自由空间传播模型
        signal_strength = np.maximum(signal_strength, signal)

    return X, Y, signal_strength

# 生成信号热力图
def plot_signal_heatmap(X, Y, signal_strength):
    plt.figure(figsize=(10, 8))
    plt.contourf(X, Y, signal_strength, levels=np.arange(-100, -50, 1), cmap='viridis')
    plt.colorbar(label='Signal Strength (dBm)')
    plt.title('WiFi Signal Heatmap')
    plt.xlabel('X (m)')
    plt.ylabel('Y (m)')
    plt.show()

# 主程序
ap_positions = deploy_ap()
X, Y, signal_strength = calculate_signal_strength(ap_positions)
plot_signal_heatmap(X, Y, signal_strength)

# 输出 AP 坐标和信道分配
print("AP Positions:", ap_positions)
print("Channel Assignment:")
for i, ap in enumerate(ap_positions):
    print(f"AP {i+1}: Position={ap}, Channel=2.4GHz (Channel {i+1})")